<?php

namespace app\common\model;

use app\common\model\Machine as MachineM;
use think\Db;
use think\Model;
use think\Request;

/**
 * 矿机模型
 */
class Machine extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    protected static function init()
    {
    }

    /**
     * 读取分类列表
     * @param string $type   指定类型
     * @param string $status 指定状态
     * @return array
     */
    public static function getCategoryArray($type = null, $status = null)
    {
        $list = collection(self::where(function ($query) use ($type, $status) {
            if (!is_null($type)) {
                $query->where('type', '=', $type);
            }
            if (!is_null($status)) {
                $query->where('status', '=', $status);
            }
        })->order('weigh', 'desc')->select())->toArray();
        return $list;
    }

    /**
     * 获取列表
     */
    public static function machine_list()
    {
        return Machine::all();
    }

    /**
     * 购买矿机
    */
    public static function buy_machine($machine_id, $uid)
    {
        $machine = MachineM::get($machine_id);
        if (!$machine) {
            return ['error' => 1, 'msg' => '该矿机已被销毁'];
        }
        $machine_category = MachineCategory::get($machine['cid']);
        $balance = Db::name('wallet')->where(['uid' => $uid, 'coin_id' => $machine_category['buy_coin_id']])->value('balance');
        if (!$balance) {
            return ['error' => 10000, 'msg' => '您的钱包信息不存在'];
        } elseif ($balance <= $machine['price']) {
            return ['error' => 1, 'msg' => '您的余额不足'];
        } else {
            $res = self::add_order($uid, $machine);
            if ($res['error'] > 0) {
                return ['error' => 1, 'msg' => $res['msg']];
            }

            $res = Wallet::up_coin($uid, -$machine['price'], $machine['buy_coin_id'], \request()->action(), $res['data']);
            if ($res['error'] > 0) {
                return ['error' => 1, 'msg' => $res['msg']];
            }
        }

        return ['error' => 0, 'msg' => '购买成功'];
    }

    /**
     * 添加矿机订单
    */
    public static function add_order($uid, $machine, $amount = 1)
    {
        if (is_integer($machine)) {
            $machine = Machine::get($machine);
        }
        if (!$machine) {
            return ['error' => 1, 'msg' => '矿机不存在'];
        }
        $data = [
            'uid' => $uid,
            'name' => $machine['name'],
            'image' => $machine['image'],
            'price' => $machine['price'] * $amount,
            'c_power' => $machine['c_power'] * $amount,
            'buy_coin_id' => $machine['buy_coin_id'],
            'createtime' => time(),
            'updatetime' => time(),
            'status' => 'normal',
        ];
        $res = Db::name('machine_order')->insertGetId($data);
        if ($res) {
            return ['error' => 0, 'msg' => '成功', 'data' => $res];
        } else {
            return ['error' => 1, 'msg' => '新增订单失败'];
        }
    }
}
